
package com.craterhill.java.ems.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.PreparedStatementCreator;

import com.craterhill.java.ems.common.beans.Session;
import com.craterhill.java.ems.dao.impl.AbstractDao;
import com.craterhill.java.ems.dao.utils.EMSResultSetExtractor;

public interface SessionDao extends EMSDao<Session> {

	public final String FIELD_LIST = "obj.session_id, obj.session_name, obj.start_date, obj.end_date" + AbstractDao.MONITER_FIELDS;

	public final String FIND_SESSION_BY_SCHOOL = "select " + FIELD_LIST + " from session obj where obj.school_id = ?";
	public final String FIND_SESSION_BY_ID = "select " + FIELD_LIST + " from session obj where obj.session_id = ?";
	
	public final String FIND_ALL_SESSION = "select " + FIELD_LIST + " from session obj";

	public final String SAVE_SESSION = "insert into session (session_name, status, start_date, end_date, created_by) values (?, ?, ?, ?, ?)";
	public final String UPDATE_SESSION = "update session set session_name = ?, status = ?, start_date = ?, end_date = ?, modified_date = ? where session_id = ?";
	public final String DELETE_SESSION = "delete from session where session_id = ?";

	public List<Session> findBySchool(final int schoolId);

	public final class SessionExtractor extends EMSResultSetExtractor<Session> {

		@Override
		protected Session extractResultSetData(ResultSet resultSet) throws SQLException {
			Session session = new Session();
			session.setSessionId(resultSet.getInt(1));
			session.setSessionName(resultSet.getString(2));
			session.setStateDate(resultSet.getDate(3));
			session.setEndDate(resultSet.getDate(4));

			session.setStatus(resultSet.getBoolean(5));
			session.setCreatedBy(resultSet.getInt(6));
			session.setCreatedDate(resultSet.getDate(7));
			session.setModifiedDate(resultSet.getDate(8));
			return session;
		}
	}
	
	public final class SessionPreparedStatementCreator implements PreparedStatementCreator {

		private final int ACTION;
		private final Session session;

		public SessionPreparedStatementCreator(final int ACTION, final Session session) {
			this.ACTION = ACTION;
			this.session = session;
		}

		@Override
		public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
			PreparedStatement preparedStatement = connection.prepareStatement(this.ACTION == EMSDao.ACTION_SAVE ? SAVE_SESSION : UPDATE_SESSION);
			preparedStatement.setString(1, session.getSessionName());
			preparedStatement.setBoolean(2, session.isStatus());
			preparedStatement.setDate(3, new Date(session.getStateDate().getTime()));
			preparedStatement.setDate(4, new Date(session.getEndDate().getTime()));

			if(this.ACTION == EMSDao.ACTION_SAVE){
				preparedStatement.setInt(5, session.getCreatedBy());
			} else{
				preparedStatement.setDate(5, session.getModifiedDate());
				preparedStatement.setInt(6, session.getSessionId());
			}

			return preparedStatement;
		}
	}
}
